package com.demo.webflux.simple;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@RestController
@RequestMapping("/product")
public class ProductController {

	@Autowired
	private ProductService productService;

    @GetMapping("")
	public Flux<Product> getProducts(){
		return productService.getProducts();
	}

    @GetMapping("/{id}")
    public Mono<Product> getProductById(@PathVariable("id") final String id){
    	return productService.getProductById(id);
    }

    @PostMapping("")
    public Mono<Void> createProduct(@RequestBody final Mono<Product> productMono){
    	return productService.createOrUpdateProduct(productMono);
    }

    @DeleteMapping("/{id}")
    public Mono<Product> delete(@PathVariable("id") final String id){
    	return productService.deleteProduct(id);
    }

}